import Vue from "vue";
import VueRouter from "vue-router";
// import login from "../views/login.vue";

Vue.use(VueRouter);
const routes = [
  {
    path: "/",
    name: "Login",
    component: resolve => require(["../views/login.vue"], resolve)
  },
  {
    path: "/Home",
    name: "Home",
    redirect: "Welcome",
    meta: {
      requireAuth: true
    },
    component: resolve => require(["../views/Home.vue"], resolve),
    children: [
      {
        path: "/regist",
        name: "Regist",
        component: resolve => require(["../views/regist.vue"], resolve)
      },
      {
        path: "/insureCompaManage",
        name: "InsureCompaManage",
        component: resolve =>
          require(["../views/insureCompaManage.vue"], resolve)
      },
      {
        path: "/welcome",
        name: "Welcome",
        component: resolve => require(["../views/welcome.vue"], resolve)
      },
      {
        path: "/carBrand",
        name: "Carbrand",
        component: resolve => require(["../views/carBrand.vue"], resolve)
      },
      {
        path: "/sshop",
        name: "Sshop",
        component: resolve => require(["../views/sshop.vue"], resolve)
      },
      {
        path: "/repairdepot",
        name: "Repairdepot",
        component: resolve => require(["../views/repairdepot.vue"], resolve)
      },
      {
        path: "/fastrepairfactory",
        name: "Fastrepairfactory",
        component: resolve =>
          require(["../views/fastrepairfactory.vue"], resolve)
      },
      {
        path: "/user",
        name: "User",
        component: resolve => require(["../views/user.vue"], resolve)
      },
      {
        path: "/driver",
        name: "Driver",
        component: resolve =>
          require(["../views/driver.vue"], resolve)
      },
      {
        path: "/driverCard",
        name: "DriverCard",
        component: resolve => require(["../views/driverCard.vue"], resolve)
      },
      {
        path: "/etcApply",
        name: "EtcApply",
        component: resolve => require(["../views/etcApply.vue"], resolve)
      },
      {
        path: "/insuremanage",
        name: "Insuremanage",
        component: resolve => require(["../views/insurance.vue"], resolve)
      },
      {
        path: "/insuranceAdd",
        name: "InsuranceAdd",
        component: resolve => require(["../views/insuranceAdd.vue"], resolve)
      },
      {
        path: "/remote",
        name: "Remote",
        component: resolve => require(["../views/Remote.vue"], resolve)
      },
      {
        path: "/newcar",
        name: "Newcar",
        component: resolve => require(["../views/Newcar.vue"], resolve)
      },
      {
        path: "/order",
        name: "Order",
        component: resolve => require(["../views/order.vue"], resolve)
      }
    ]
  },
  {
    path: "/forgetpass",
    name: "Forgetpass",
    component: resolve => require(["../views/forgetpass.vue"], resolve)
  }
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes
});
router.beforeEach((to, from, next) => {
  // 访问登录页，放行
  if (to.path === '/' || to.path === '/forgetpass') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  // 没有token, 强制跳转到登录页
  if (!tokenStr) return next('/')
  next()
})
export default router;
